package main

import (
	"fmt"
	"gitee.com/Cookie_XiaoD/go-filestore-server/handlers"
	"gitee.com/Cookie_XiaoD/go-filestore-server/middleware"
	"net/http"
)

func main() {
	initFileHandlers()
	initUserHandlers()
	initMPUploadHandlers()

	//无需token验证的请求路径
	noAuthPath := []string{"/user/signup", "/user/signin", "/views", "/static"}

	//处理静态视图资源
	http.Handle("/views/", http.StripPrefix("/views/", http.FileServer(http.Dir("../../static/view"))))
	//处理其他静态资源
	http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("../../static"))))

	logMw := middleware.NewLogMiddleware(http.DefaultServeMux) //日志中间件
	jwtMw := middleware.NewTokenMiddleware(logMw, noAuthPath)  //token验证中间件
	corsMw := middleware.NewCORSMiddleware(jwtMw)              //cors跨域中间件

	err := http.ListenAndServe(":40001", corsMw)
	//ListenAndServe方法是阻塞的，因此只有失败时才会执行如下代码
	fmt.Printf("监听HTTP服务失败：%v", err.Error())
}

//初始化文件相关接口
func initFileHandlers() {
	//文件上传
	http.HandleFunc("/file/upload", handlers.UploadHandler)
	//文件上传成功
	http.HandleFunc("/file/upload/success", handlers.UploadSuccessHandler)
	//获取文件meta
	http.HandleFunc("/file/meta", handlers.GetFileMetaHandler)
	//下载文件
	http.HandleFunc("/file/download", handlers.DownloadFileHandler)

	//更新文件元信息
	http.HandleFunc("/file/update", handlers.UpdateFileMetaHandler)

	//删除文件
	http.HandleFunc("/file/delete", handlers.DeleteFileHandler)

	//查询用户文件
	http.HandleFunc("/file/query", handlers.QueryFileMetasByUserNameHandler)

	//秒传
	http.HandleFunc("/file/fastupload", handlers.TryFastUploadHandler)
	//获取下载URL
	http.HandleFunc("/file/downloadurl", handlers.GetFileDownloadURL)

}

//初始化用户相关接口
func initUserHandlers() {
	//用户注册
	http.HandleFunc("/user/signup", handlers.UserSignup)

	//用户登录
	http.HandleFunc("/user/signin", handlers.UserSignIn)

	//用户主页
	http.HandleFunc("/user/home", handlers.UserHome)
	//用户信息
	http.HandleFunc("/user/info", handlers.UserInfo)
}

//初始化分块上传相关接口
func initMPUploadHandlers() {
	//分块上传初始化
	http.HandleFunc("/file/mpupload/init", handlers.InitMultipartUploadHandler)

	//分块上传
	http.HandleFunc("/file/mpupload/uppart", handlers.UploadPartHandler)

	//分块上传完成
	http.HandleFunc("/file/mpupload/complete", handlers.CompleteUploadHandler)
}
